package com.zlk.algorithm.huawei.leetcode.list;

import com.zlk.algorithm.util.ListNode;
import org.junit.Test;

/**
 * @program: algorithm
 * @ClassName Code16_reverseList
 * @description:反转链表
 * @author: slfang
 * @create: 2025-01-07 14:12
 * @Version 1.0
 **/
public class Code16_reverseList {

    public ListNode reverseList(ListNode head) {
        if(head==null||head.next==null){
            return head;
        }
        ListNode pre = null;
        ListNode cur = head;
        while (cur.next!=null){
            ListNode next = cur.next;
            cur.next = pre;
            pre = cur;
            cur =next;
        }
        cur.next=pre;
        return cur;
    }

    public ListNode reverseList2(ListNode head) {
        ListNode prev = null;
        ListNode curr = head;
        while (curr != null) {
            ListNode next = curr.next;
            curr.next = prev;
            prev = curr;
            curr = next;
        }
        return prev;
    }


    @Test
    public void test(){
        ListNode headA = new ListNode(1);
        headA.next = new ListNode(2);
        headA.next.next = new ListNode(3);
        headA.next.next.next = new ListNode(4);
        reverseList(headA);
    }


}
